package com.lizemin.controller;

import com.lizemin.entity.BroadcastMessage;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BroadcastController {

    private final SimpMessagingTemplate messagingTemplate;

    public BroadcastController(SimpMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    @PostMapping("/broadcast")
    public void broadcastMessage(@RequestBody BroadcastMessage message) {
        // 向 /topic/broadcast 主题发送消息，所有订阅该主题的客户端都会收到消息
        messagingTemplate.convertAndSend("/topic/broadcast", message);
    }
}